//
//  BaseButton.swift
//  XingCeHelper
//
//  Created by 申铭 on 2023/1/13.
//

import UIKit

public class BaseButton: UIButton {
    /// 正常状态时颜色
    public var normalColor: UIColor? {
        didSet {
            backgroundColor = normalColor
        }
    }
    /// 高亮颜色
    public var highlightedColor: UIColor?

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        addMethod()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        
        setupUI()
        addMethod()
    }
    
    private func setupUI() {
        setFillet()
        normalColor = .systemBlue
        highlightedColor = UIColor(hexString: "#0a50d8")
        titleLabel?.textColor = .white
    }
    
    private func addMethod() {
        addTarget(self, action: #selector(btnTouchDown), for: .touchDown)
        addTarget(self, action: #selector(btnTouchUpInside), for: .touchUpInside)
        addTarget(self, action: #selector(btnTouchUpInside), for: .touchDragExit)
        addTarget(self, action: #selector(btnTouchDown), for: .touchDragEnter)
    }
    
    @objc private func btnTouchDown() {
        backgroundColor = highlightedColor
        transform = CGAffineTransform(scaleX: 0.98, y: 0.98)
    }
    
    @objc private func btnTouchUpInside() {
        backgroundColor = normalColor
        transform = CGAffineTransform.identity
    }

}
